//
//  THMyPublishCell.m
//  YJB
//
//  Created by 中电大维 on 2017/1/5.
//  Copyright © 2017年 YJB. All rights reserved.
//

#import "THCPCMyPublishCell.h"

NSString *const cpcMyPublishID = @"cpcMyPublishID";

@implementation THCPCMyPublishCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        _model = [[THCpcMyPublishModel alloc] init];
        [self addSubview:self.iconImgView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.awardLabel];
        [self addSubview:self.detailButton];
        [self addSubview:self.completeLabel];
        [self addSubview:self.surplusLabel];
        [self addSubview:self.deleteButton];
        [self setFrame];
        [self cpcTest];
    }
    return self;
}

- (NSMutableAttributedString *)getAttributedString:(NSString *)front Middle:(NSString *)middle Behind:(NSString *)behind{
    
    NSMutableAttributedString *attr = [NSString attributedString:front frontColor:kUIColorFromRGB(0x333333) middle:middle middleColor:kUIColorFromRGB(0xff783c) behind:behind behindColor:kUIColorFromRGB(0x333333) font:FONT(15)];
    return attr;
}

- (void)setModel:(THCpcMyPublishModel *)model
{
    _model = model;
    self.titleLabel.text = model.adv_title;
    self.awardLabel.text = [NSString stringWithFormat:@"%@金币",model.gold_one];
//    self.completeLabel.text = [NSString stringWithFormat:@"完成%@个",model.complete_num];
//    self.surplusLabel.text = [NSString stringWithFormat:@"剩余%@个",model.leftover_num];
    
    self.completeLabel.attributedText = [self getAttributedString:@"完成" Middle:[model.complete_num intValue]?model.complete_num:@"0" Behind:@"个"];
    self.surplusLabel.attributedText = [self getAttributedString:@"剩余" Middle:[model.leftover_num intValue]?model.leftover_num:@"0" Behind:@"个"];
    
    NSString *urlString = model.adv_icon;
    if (![urlString hasPrefix:@"http"])
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];
}

- (void)deleteMyPublishItem:(UIButton *)button
{
    [self.delegate deleteMyPublishItem:button];
}
- (void)detailItem:(UIButton *)button
{
    [self.delegate detailItem:button];
}
- (void)setFrame
{
    NSString *detailString = @"查看完成详情 >>";
    NSString *award = [NSString stringWithFormat:@"100000金币"];
    
    CGFloat detailWidth = [NSString widthForString:detailString fontSize:14 andHeight:15];
    CGFloat awardWidth = [NSString widthForString:award UIfontSize:[UIFont boldSystemFontOfSize:16] andHeight:15];
    
    [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(20);
        make.left.equalTo(self.mas_left).offset(15);
        make.width.height.mas_equalTo(48);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImgView.mas_top).offset(-2);
        make.left.equalTo(self.iconImgView.mas_right).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.equalTo(self.iconImgView.mas_height);
    }];
    [self.awardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(2);
        make.left.equalTo(self.titleLabel.mas_left);
        make.width.mas_equalTo(awardWidth);
        make.height.mas_equalTo(20);
    }];
    [self.detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.awardLabel.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-12);
        make.width.mas_equalTo(detailWidth);
        make.height.mas_equalTo(28);
    }];
    [self.completeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.awardLabel.mas_left);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(28);
        make.top.equalTo(self.awardLabel.mas_bottom).offset(10);
    }];
    [self.surplusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.completeLabel.mas_right).offset(15);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(self.completeLabel.mas_height);
        make.centerY.equalTo(self.completeLabel.mas_centerY);
    }];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.surplusLabel.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-12);
        make.width.mas_equalTo(52);
        make.height.mas_equalTo(self.surplusLabel.mas_height);
    }];
}
- (void)cpcTest
{
    self.iconImgView.image = Image(@"th_cpc_default_normal");
    self.titleLabel.text = @"只要轻松点几下，钱就到手啦，亲还在等什么呢，赶紧点我吧...";
    self.awardLabel.text = @"12金币";
    
    self.completeLabel.text = @"完成500个";
    self.surplusLabel.text = @"剩余1000个";
    
//    self.completeLabel.backgroundColor = [UIColor orangeColor];
//    self.surplusLabel.backgroundColor = [UIColor orangeColor];
}

-(UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.image = Image(@"th_cpc_default_normal");
    }
    return _iconImgView;
}

-(UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textColor = kUIColorFromRGB(0x333333);
        
    }
    return _titleLabel;
}

-(UILabel *)awardLabel
{
    if (_awardLabel == nil)
    {
        _awardLabel = [[UILabel alloc]init];
        _awardLabel.backgroundColor = [UIColor clearColor];
        _awardLabel.font = [UIFont boldSystemFontOfSize:17];
        _awardLabel.numberOfLines = 0;
        _awardLabel.textColor = kUIColorFromRGB(0xfa5f3c);
       
    }
    return _awardLabel;
}

- (UIButton *)detailButton
{
    if (!_detailButton) {
        _detailButton = [[UIButton alloc] init];
        [_detailButton setTitleColor:kUIColorFromRGB(0xfa5f3c) forState:UIControlStateNormal];
        _detailButton.titleLabel.font = FONT(14);
        _detailButton.adjustsImageWhenHighlighted = NO;
        [_detailButton setTitle: @"查看完成详情 >>" forState:UIControlStateNormal];
        [_detailButton addTarget:self action:@selector(detailItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailButton;
}

- (UILabel *)completeLabel
{
    if (_completeLabel == nil)
    {
        _completeLabel = [[UILabel alloc]init];
        _completeLabel.font = FONT(14);
        _completeLabel.textColor = kUIColorFromRGB(0xfa5f3c);
        _completeLabel.textAlignment = NSTextAlignmentCenter;
        [_completeLabel.layer setMasksToBounds:YES];
        [_completeLabel.layer setCornerRadius:5.0];
        [_completeLabel.layer setBorderWidth:1];
        [_completeLabel.layer setBorderColor:kUIColorFromRGB(0xfa5f3c).CGColor];
    }
    return _completeLabel;
}

-(UILabel *)surplusLabel
{
    if (_surplusLabel == nil)
    {
        _surplusLabel = [[UILabel alloc]init];
        _surplusLabel.font = FONT(15);
        _surplusLabel.textColor = kUIColorFromRGB(0xfa5f3c);
        _surplusLabel.textAlignment = NSTextAlignmentCenter;
        [_surplusLabel.layer setMasksToBounds:YES];
        [_surplusLabel.layer setCornerRadius:5.0];
        [_surplusLabel.layer setBorderWidth:1];
        [_surplusLabel.layer setBorderColor:kUIColorFromRGB(0xfa5f3c).CGColor];
    }
    return _surplusLabel;
}
- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        _deleteButton = [[UIButton alloc] init];
        _deleteButton.titleLabel.font = FONT(15);
        _deleteButton.adjustsImageWhenHighlighted = NO;
        _deleteButton.backgroundColor = [UIColor colorWithHexString:@"ff783c"];
        [_deleteButton setTintColor:[UIColor whiteColor]];
        [_deleteButton.layer setMasksToBounds:YES];
        [_deleteButton.layer setCornerRadius:3.0];
        [_deleteButton setTitle: @"删除" forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteMyPublishItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}


@end
